package org.shopen.ai.puremvc.model.project
{
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	import org.shopen.ai.puremvc.model.project.vo.Project;
	import org.shopen.ai.puremvc.model.project.vo.ProjectMeta;
	import org.shopen.ai.puremvc.model.utils.CompleteDelegate;

	public class ProjectsProxy extends Proxy
	{
		public static const NAME : String 						= "ProjectsProxy";
		private static const delim : String 					= ".";
		public static const PROJECTS_INITIALIZED : String 		= NAME + delim + "PROJECTS_INITIALIZED";
		public static const PROJECTS_META_INITIALIZED : String 	= NAME + delim + "PROJECTS_META_INITIALIZED";
		
		public function ProjectsProxy()
		{
			super(ProjectsProxy.NAME);
		}
		
		override public function getProxyName():String
		{
			return ProjectsProxy.NAME;
		}
		
		override public function onRegister():void
		{
			initializeData();
		}
		
		public function get projectsMeta() : ArrayCollection
		{
			var result : ArrayCollection = new ArrayCollection();
			for each (var project : Project in this.projects)
			{
				result.addItem(project.meta);
			}
			return result;
		}
		
		public function get projects() : ArrayCollection
		{
			var result : ArrayCollection = getData() as ArrayCollection;
			return result;
		}
		
		public function getProject(projectId : String) : Project
		{
			var result : Project = null;
			for each(var project : Project in this.projects)
			{
				if (project.meta.id == projectId)
				{
					result = project;
					break;
				}
			}
			return result;
		}
		
		
		protected function initializeData() : void
		{
			var projectMeta : ProjectMeta = new ProjectMeta();
			projectMeta.id = "project.cities.1";
			projectMeta.title = "Cities Problem";
			var project : Project = new Project();
			project.meta = projectMeta;
			project.steps = new ArrayCollection(["Preview Model", "Setup & Customize", "Searching", "Results & Statistics"])
			
			var array : ArrayCollection = new ArrayCollection();
			array.addItem(project);
			setData(array);
		}
		
		public function loadData(completeDelegate : CompleteDelegate = null) : void
		{
			sendNotification(ProjectsProxy.PROJECTS_INITIALIZED, projects);
			sendNotification(ProjectsProxy.PROJECTS_META_INITIALIZED, projectsMeta);
			if (completeDelegate != null)
			{
				completeDelegate.dispatchComplete();
			}
		}
		
		private var currentProjectId : String = null;
		public function setCurrentProject(id : String) :void
		{
			currentProjectId = id;
		}
		
		public function getCurrentProject() : Project
		{
			var result : Project = null;
			result = getProject(currentProjectId);			
			return result;
		}
	}
}